package com.zyx.javademo.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * @author Yaxi.Zhang
 * @since 2021/5/28 10:47
 * desc: 将接口对象转化为流
 */
public class IteratorToStreamDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("zyx");
        list.add("java");
        list.add("flink");
        list.add("spark");
        list.add("hadoop");

        Iterator<String> iterator = list.iterator();

        // Iterator -> Spliterators -> Stream -> List
        //  Iterator接口没有spliterator()方法，因此我们需要使用Spliterators.spliteratorUnknownSize将Iterator转换为Spliterator
        //  然后使用StreamSupport.stream将Spliterator转换为Stream 。
        List<Integer> result = StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
                .map(String::length)
                .collect(Collectors.toList());

        result.forEach(System.out::println);
        System.out.println(result.getClass());
    }
}
